<template>
    <div class="rename-dialog">
        <el-dialog
            :title="title"
            width="30%"
            :visible.sync="dialogVisible">
            <el-input type="textarea" v-model="form.name"></el-input>
            <div v-if="!isUpdate && form.attr === '2' " style="margin-top: 20px">
                <span>是否是数据大屏</span>
                <el-switch v-model="isDataScreen"></el-switch>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
                <el-button type="primary" @click="upName(form)" size="mini">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import ajax from '@api/ajax'
    import {DASH_BORARD} from '@api/url'
    export default {
        name: 'renameDialog',
        data () {
            return {
                dialogVisible: false,
                form: {
                    name:'',
                },
                title:'请输入名称',
                isUpdate:false,
                isDataScreen:false
            }
        },
        methods: {
            openDialog (val) {
                // 新建 仪表盘
                // 重命名
                if(val && val.menuId){
                    this.title = '重命名'
                    this.isUpdate = true
                }else{
                    this.title = '请输入名称'
                    this.isUpdate = false
                }
                this.dialogVisible = true
                this.form = JSON.parse(JSON.stringify(val)) || {}
            },
            upName (val) {
                let param = {
                    optMode: 'A',
                    type: '6',
                    name: val.name,
                    attr: val.attr, // 1文件夹 2仪表盘 3 数据大屏
                    parentId: val.parentId,
                }
                if(this.isDataScreen){
                    param.attr = '3';
                }
                if(this.isUpdate){
                    param = Object.assign(param,{
                        optMode:'U',
                        menuId: val.menuId,
                        parentId: val.parentId,
                        type: val.type,
                    })
                }
                ajax.post(DASH_BORARD, param).then(res => {
                    this.$message.success('保存成功！')
                    this.$emit('saved')
                }).catch(err => {
                    console.error('操作失败！', err)
                }).finally(() => {
                    this.dialogVisible = false
                })
            }
        }
    }
</script>

<style scoped>

</style>
